<template>
  <div class="main-content">
    <h1>楼盘销售表</h1>
    <div class="screen-frame clearfix">
      <div class="row">
        <div class="col-lg-4">
          <div class="form-horizontal">
            <div class="form-group">
              <label class="col-md-4 col-sm-2 control-label">楼盘名称：</label>
              <div class="col-md-7 col-sm-5">
                <el-select class=""
                          v-model="params.premisesId"
                          filterable
                          placeholder="请选择"
                          :clearable="true"
                          style="width:100%">
                  <el-option v-for="item in findPremisesList"
                            :key="item.premisesId"
                            :label="item.premisesName"
                            :value="item.premisesId">
                  </el-option>
                </el-select>
              </div>
            </div>
          </div>
        </div>
        <div class="col-lg-4">
          <div class="form-horizontal">
            <div class="form-group">
              <label class="col-md-4 col-sm-2 control-label">时间区间：</label>
              <div class="col-md-4 col-sm-5">
                <DatePicker type="date"
                            placeholder="开始日期"
                            v-model="params.beginDate"
                            style="width: 120px"></DatePicker>
              </div>
              <div class="col-md-4 col-sm-5">
                <DatePicker type="date"
                            placeholder="结束日期"
                            v-model="params.endDate"
                            style="width: 120px"></DatePicker>
              </div>
            </div>
          </div>
        </div>
        <div class="col-lg-4">
          <div class="form-horizontal">
            <div class="form-group">
              <label class="col-md-4 col-sm-2 control-label">有效数据：</label>
                <div class="col-lg-7 col-md-2">
                  <Checkbox v-model="showValidDataFlag">过滤无业绩统计</Checkbox>
                </div>
            </div>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="col-lg-6">
          <div class="form-horizontal">
            <div class="form-group">
              <label class="col-md-4 col-sm-2 control-label">楼盘状态：</label>
                <div class="col-lg-2 col-md-2" v-for="(row, index) in premisesStatusEnum">
                  <Checkbox v-model="row.saleStatusFlag">{{ row.saleStatusName }}</Checkbox>
                </div>
            </div>
          </div>
        </div>
        <div class="col-lg-4">
          <div class="form-horizontal">
            <div class="form-group">
              <label class="col-md-3 col-sm-2 control-label"></label>
              <div class="col-md-7 col-sm-5">
                <button type="button"
                        class="btn btn-theme"
                        v-on:click="findList()">查询</button>
                <button type="button"
                        class="btn btn-theme"
                        v-if="exportBtnShow"
                        v-on:click="exportInfo"
                        v-res="{code:'menu_0007_0002_0001'}">导出</button>
                <button type="button"
                        class="btn btn-theme"
                        v-if="!exportBtnShow"
                        style="background-color: #999;"
                        v-on:click="exportRepeatMsg"
                        v-res="{code:'menu_0007_0002_0001'}">导出{{ timeOut }}</button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="row"
         style="text-align: center;color: #f87242;"
         v-show="!show">
      没有查询到相应的数据
    </div>
    <div class="table table-striped table-bordered"
         v-show="show">

      <div class="table-responsive">
        <table class="table table-striped table-bordered min-width-790">
          <thead>
            <tr>
              <th width="14%">楼盘名称</th>
              <th width="6%">报备客户</th>
              <th width="8%">报备成功客户</th>
              <th width="6%">首访客户</th>
              <th width="6%">复访客户</th>
              <th width="6%">认筹客户</th>
              <th width="6%">认购客户</th>
              <th width="6%">签约客户</th>
              <th width="6%">直签客户</th>
              <th width="6%">签约率</th>
              <th width="8%">签约业绩(元)</th>
              <th width="8%">查看</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="(row,index) in customerConversionList">
              <td style="text-align: left; font-weight: bold;">{{row.premisesName}}</td>
              <td>{{row.countOne}}</td>
              <td>{{row.countTwo}}</td>
              <td>{{row.countThree}}</td>
              <td>{{row.countNine}}</td>
              <td>{{row.countFour}}</td>
              <td>{{row.countFive}}</td>
              <td>{{row.countSix}}</td>
              <td>{{row.countSeven}}</td>
              <td>{{utils.calculateSignRate(row.countTwo,row.countSix)}}</td>
              <td>{{row.dealAmount}}</td>
              <td><a class="a-report"
                   @click="gotoEffectReport(row.premisesId)">查看销售分布</a></td>
            </tr>

            <tr class="result-total">
              <td>合计</td>
              <td>{{resultTotal.countOne}}</td>
              <td>{{resultTotal.countTwo}}</td>
              <td>{{resultTotal.countThree}}</td>
              <td>{{resultTotal.countNine}}</td>
              <td>{{resultTotal.countFour}}</td>
              <td>{{resultTotal.countFive}}</td>
              <td>{{resultTotal.countSix}}</td>
              <td>{{resultTotal.countSeven}}</td>
              <td></td>
              <td>{{resultTotal.dealAmount}}</td>
              <td></td>
            </tr>
          </tbody>
        </table>
      </div>
      <div id="laypage_cus_conversion_report"></div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      params: {
        pageNo: 1,
        pageSize: 10,
        beginDate: this.utils.getCurrentMonthFirst(),
        endDate: this.utils.getCurrentMonthLast(),
        premisesId: "",
        saleStatus: "1",
        validateData: true,
      },
      customerConversionList: [],
      findPremisesList: [],
      show: true,
      premisesStatusEnum: [
        { saleStatus: 0, saleStatusName: "待售", saleStatusFlag: false },
        { saleStatus: 1, saleStatusName: "在售", saleStatusFlag: true },
        { saleStatus: 2, saleStatusName: "售罄", saleStatusFlag: false },
        { saleStatus: 3, saleStatusName: "下架", saleStatusFlag: false },
      ],
      showValidDataFlag: true,
      resultTotal: {
        countEight: 0,
        countSeven: 0,
        countOne: 0,
        countTwo: 0,
        countThree: 0,
        countFour: 0,
        countFive: 0,
        countSix: 0,
        countNine: 0,
        dealAmount: 0,
      },
      exportBtnShow: true,
      timeOut: 10,
    };
  },
  mounted: function () {
    this.list();
    this.findPremisesLists();
  },
  methods: {
    list: function () {
      var loadIndex = layer.load();
      var _self = this;
      var url = this.utilHelper.apiUrl + "/api/getCustomerConversionPage";
      let body = {};
      body.params = _self.params;
      body.params.beginDate = this.utils.dateFormat(
        body.params.beginDate,
        "yyyy-MM-dd"
      );
      body.params.endDate = this.utils.dateFormat(
        body.params.endDate,
        "yyyy-MM-dd"
      );
      let intervalDays = this.utils.intervalTime(body.params.beginDate, body.params.endDate);
      if(intervalDays > 31){
        layer.close(loadIndex);
        layer.msg("单次最多获取31天的数据!");
        return false;
      }
      let saleStatus = "";
      _self.premisesStatusEnum.map((val, index, arr) => {
        if (val.saleStatusFlag) {
          saleStatus += val.saleStatus + ",";
        }
      });
      if (saleStatus) {
        saleStatus = saleStatus.substring(0, saleStatus.length - 1);
      } else {
        layer.close(loadIndex);
        layer.msg("请至少选中1个楼盘状态!");
        return false;
      }
      body.params.saleStatus = saleStatus;
      body.params.validateData = _self.showValidDataFlag;
      _self.$http.post(url, body).then((response) => {
        layer.close(loadIndex);
        var body = response.body.resData;
        if (body.result == 0) {
          layer.msg(body.resultMsg);
        } else {
          _self.customerConversionList = body.pageModel.rows;
          //统计数据
          _self.setResultTotoal();
          var totalPages = body.pageModel.totalPages;
          var current = body.pageModel.pageNumber;
          var total = body.pageModel.total;
          _self.show = total > 0 ? true : false;
          // if(totalPages > 0) {
          layui.laypage.render({
            elem: $("#laypage_cus_conversion_report"), //容器。值支持id名、原生dom对象，jquery对象,
            limit: _self.params.pageSize,
            curr: current,
            count: total,
            theme: "#f87242;", //皮肤
            first: "首页", //若不显示，设置false即可
            last: "尾页", //若不显示，设置false即可
            prev: "上一页", //若不显示，设置false即可
            next: "下一页", //若不显示，设置false即可
            limits: [10, 20, 50, 100, 200],
            layout: ["prev", "page", "next", "skip", "count", "limit"],
            jump: function (obj, first) {
              if (!first) {
                _self.params.pageNo = obj.curr;
                _self.params.pageSize = obj.limit;
                _self.list();
              }
            },
          });
          // }
        }
      });
    },
    findPremisesLists: function () {
      var _self = this;
      var url = this.utilHelper.apiUrl + "/api/getCaseMemberList";
      var body = {};
      body.params = {
        userId: window.localStorage.getItem("userId"),
      };
      _self.$http.post(url, body).then((response) => {
        _self.findPremisesList = response.body.resData.caseMemberList;
      });
    },
    exportInfo: function () {
      var loadIndex = layer.load();
      var _self = this;

      if (
        !_self.customerConversionList ||
        _self.customerConversionList.length == 0
      ) {
        layer.msg("销售表中无可导出数据!");
        return false;
      }
      var userId = window.localStorage.getItem("1_userId");
      var beginDate = this.utils.dateFormat(
        this.params.beginDate,
        "yyyy-MM-dd"
      );
      var endDate = this.utils.dateFormat(this.params.endDate, "yyyy-MM-dd");

      let intervalDays = this.utils.intervalTime(beginDate, endDate);
      if(intervalDays > 31){
        layer.close(loadIndex);
        layer.msg("单次最多获取31天的数据!");
        return false;
      }

      var premisesId = "";
      if (_self.params.premisesId) {
        premisesId = _self.params.premisesId;
      }

      let saleStatus = "";
      _self.premisesStatusEnum.map((val, index, arr) => {
        if (val.saleStatusFlag) {
          saleStatus += val.saleStatus + ",";
        }
      });
      if (saleStatus) {
        saleStatus = saleStatus.substring(0, saleStatus.length - 1);
      } else {
        layer.close(loadIndex);
        layer.msg("请至少选中1个楼盘状态!");
        return false;
      }

      var validateData = _self.params.validateData;
      _self.exportBtnShow = false;
      var url =
        this.utilHelper.apiUrl +
        "/api/getCustomerConversionExport?params.userId=" +
        userId +
        "&params.beginDate=" +
        beginDate +
        "&params.endDate=" +
        endDate +
        "&params.premisesId=" +
        premisesId +
        "&params.saleStatus=" +
        saleStatus +
        "&params.validateData=" +
        validateData +
        "&params.pageNo=1&params.loginType=1&params.pageSize=9999999";
      location.href = url;

      var exportInterval = setInterval(function () {
        if (_self.timeOut <= 0) {
          window.clearInterval(exportInterval);
          _self.timeOut = 10;
          _self.exportBtnShow = true;
        } else {
          _self.timeOut -= 1;
        }
      }, 1000);
      layer.close(loadIndex);
    },
    exportRepeatMsg: function () {
      var _self = this;
      layer.msg(_self.timeOut + "秒后可再次导出,请稍等片刻!");
      return;
    },
    gotoEffectReport(premisesId) {
      var _self = this;
      _self.$router.push({
        name: "channelEffectReport",
        params: {
          beginDate: this.params.beginDate,
          endDate: this.params.endDate,
          premisesId: premisesId,
        },
      });
    },
    findList() {
      var _self = this;
      _self.params.pageNo = 1;
      _self.list();
    },
    setResultTotoal() {
      var _self = this;
      _self.resultTotal = {
        countEight: 0,
        countSeven: 0,
        countOne: 0,
        countTwo: 0,
        countThree: 0,
        countFour: 0,
        countFive: 0,
        countSix: 0,
        countNine: 0,
        dealAmount: 0,
      };
      for (var i in _self.customerConversionList) {
        _self.resultTotal.countOne =
          Number(_self.resultTotal.countOne) +
          Number(_self.customerConversionList[i].countOne);
        _self.resultTotal.countTwo =
          Number(_self.resultTotal.countTwo) +
          Number(_self.customerConversionList[i].countTwo);
        _self.resultTotal.countThree =
          Number(_self.resultTotal.countThree) +
          Number(_self.customerConversionList[i].countThree);
        _self.resultTotal.countFour =
          Number(_self.resultTotal.countFour) +
          Number(_self.customerConversionList[i].countFour);
        _self.resultTotal.countFive =
          Number(_self.resultTotal.countFive) +
          Number(_self.customerConversionList[i].countFive);
        _self.resultTotal.countSix =
          Number(_self.resultTotal.countSix) +
          Number(_self.customerConversionList[i].countSix);
        _self.resultTotal.countSeven =
          Number(_self.resultTotal.countSeven) +
          Number(_self.customerConversionList[i].countSeven);
        _self.resultTotal.countEight =
          Number(_self.resultTotal.countEight) +
          Number(_self.customerConversionList[i].countEight);
        _self.resultTotal.countNine =
          Number(_self.resultTotal.countNine) +
          Number(_self.customerConversionList[i].countNine);
        _self.resultTotal.dealAmount =
          Number(_self.resultTotal.dealAmount) +
          Number(_self.customerConversionList[i].dealAmount);
      }
    },
  },
};
</script>